/**
 * 
 */
package org.xlab.baseframework.web.component;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.vaadin.peter.buttongroup.ButtonGroup;
import org.xlab.baseframework.domain.ISystemUserDomain;
import org.xlab.baseframework.domain.SystemCode;
import org.xlab.baseframework.domain.view.SystemCodeVO;
import org.xlab.baseframework.exception.SystemCodeNotFoundException;
import org.xlab.baseframework.service.api.ISystemCodeService;
import org.xlab.baseframework.web.data.CodeEditFormFieldFactory;

import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Form;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * @author makubex
 * 
 */
@Controller("bpf_codeEditWindow")
@Scope("request")
public class CodeEditWindow extends Window {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = -6782343888826908825L;
	private Form editForm;
	private Item editItem;
	private Button save;
	private Button reset;
	private Button close;

	private ButtonGroup buttons;

	@Autowired(required = true)
	@Qualifier("bpf_systemCodeService")
	private ISystemCodeService systemCodeService;
	@Autowired
	@Qualifier("bpf_codeEditFormFieldFactory")
	private CodeEditFormFieldFactory codeEditFormFieldFactory;

	/**
	 * @param caption
	 */
	public CodeEditWindow(String caption) {
		super(caption);
		super.setModal(true);
		super.setClosable(true);
		super.addListener(new CloseListener() {

			/**
			 * @serialField
			 */
			private static final long serialVersionUID = 1698189262044379173L;

			public void windowClose(CloseEvent e) {
				CodeEditWindow.super.removeAllComponents();
				CodeEditWindow.this.editForm = null;
				CodeEditWindow.this.save = null;
				CodeEditWindow.this.editItem = null;
				CodeEditWindow.this.reset = null;
			}
		});
	}

	/**
	 * @see com.vaadin.ui.Panel#attach()
	 */
	public void attach() {
		super.attach();
		VerticalLayout mainLayout = new VerticalLayout();
		mainLayout.setMargin(false);
		mainLayout.setSpacing(true);
		mainLayout.setSizeFull();
		this.editForm = new Form();
		this.editForm.setWriteThrough(false);
		this.editForm.setInvalidCommitted(false);
		this.editForm.setItemDataSource(this.editItem);
		this.editForm.setFormFieldFactory(this.codeEditFormFieldFactory);
		this.editForm.setVisibleItemProperties(Arrays.asList(new String[] {
				SystemCodeVO.FIELD_CATEGORY, SystemCodeVO.FIELD_CODE,
				SystemCodeVO.FIELD_DESCRIPTION, SystemCodeVO.FIELD_SEQUENCE }));
		this.editForm.setImmediate(false);
		this.editForm.setWidth(300f, UNITS_PIXELS);
		this.buttons = new ButtonGroup();
		this.save = new Button("Save");
		this.save.addListener(new ClickListener() {

			/**
			 * @serialField
			 */
			private static final long serialVersionUID = 5291543744925218400L;

			@SuppressWarnings("unchecked")
			public void buttonClick(ClickEvent event) {
				editForm.commit();
				SystemCodeVO codeVO = ((BeanItem<SystemCodeVO>) editForm
						.getItemDataSource()).getBean();
				SystemCode codeInstance = systemCodeService.parseFromVO(codeVO);
				try {
					systemCodeService.updateSystemCode(codeInstance,
							(ISystemUserDomain) CodeEditWindow.super
									.getApplication().getUser());
					CodeEditWindow.this.close();
				} catch (SystemCodeNotFoundException e) {
					e.printStackTrace();
				}
			}
		});
		this.reset = new Button("Reset");
		this.reset.addListener(new ClickListener() {

			/**
			 * @serialField
			 */
			private static final long serialVersionUID = -3131221000775448675L;

			public void buttonClick(ClickEvent event) {
				editForm.discard();
			}
		});
		this.close = new Button("Close");
		this.close.addListener(new ClickListener() {
			
			/**
			 * @serialField
			 */
			private static final long serialVersionUID = 3469587577982053615L;

			public void buttonClick(ClickEvent event) {
				CodeEditWindow.this.close();
			}
		});
		this.buttons.addButton(save);
		this.buttons.addButton(reset);
		this.buttons.addButton(close);
		mainLayout.addComponent(this.editForm);
		mainLayout.addComponent(this.buttons);
		mainLayout.setComponentAlignment(this.editForm, Alignment.TOP_LEFT);
		mainLayout.setComponentAlignment(this.buttons, Alignment.MIDDLE_CENTER);
		super.addComponent(mainLayout);
	}

	/**
	 * @return the editItem
	 */
	public Item getEditItem() {
		return editItem;
	}

	/**
	 * @param editItem
	 *            the editItem to set
	 */
	public void setEditItem(Item editItem) {
		this.editItem = editItem;
	}

	/**
	 * @param systemCodeService
	 *            the systemCodeService to set
	 */
	public void setSystemCodeService(ISystemCodeService systemCodeService) {
		this.systemCodeService = systemCodeService;
	}

	/**
	 * @param codeEditFormFieldFactory
	 *            the codeEditFormFieldFactory to set
	 */
	public void setCodeEditFormFieldFactory(
			CodeEditFormFieldFactory codeEditFormFieldFactory) {
		this.codeEditFormFieldFactory = codeEditFormFieldFactory;
	}

	/**
	 * @see com.vaadin.ui.Window#detach()
	 */
	@Override
	public void detach() {
		super.detach();
		super.removeAllComponents();
		this.editItem = null;
		this.editForm = null;
		this.save = null;
		this.reset = null;
		this.close();
	}

}
